<?php
namespace Framework\DiServices;

use Phalcon\Events\Manager;

class Dispatcher
{
    public static function new(\Phalcon\Dispatcher $dispatcher, $defaultNamespace, array $plugins = [])
    {
        $dispatcher->setDefaultNamespace($defaultNamespace);

        if (!empty($plugins)) {
            $eventsManager = new Manager();
            foreach ($plugins as $plugin) {
                $eventsManager->attach('dispatch', $plugin);
            }

            $dispatcher->setEventsManager($eventsManager);
        }

        return $dispatcher;
    }
}
